package com.ufgov.smartclient.component.table;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;

/**
 * 功能说明：带省略号的Panel，该Panel包括了一个jLable和一个jButton
 * 创建时间：2007-7-18
 * @author 刘永伟
 * @version 1.0
 */

public abstract class EllipsisPanel extends JPanel {

  /**
   * 按钮
   */
  private JButton button = new JButton() {
    @Override
    public void paint(Graphics g) {
      super.paint(g);
      Rectangle rect = g.getClipBounds();

      //将宽度平均分为4份
      int x = rect.width / 4;
      int y = rect.height / 2;
      int width = 2; //圆点宽度
      int height = 2; //圆点高度

      //在表面上画3个圆点
      for (int i = 0; i < 3;) {
        g.fillOval(++i * x, y, width, height);
      }
    }

    @Override
    public java.awt.Dimension getPreferredSize() {
      Dimension size = EllipsisPanel.this.getSize();
      size.height -= 1;
      size.width = size.height;
      return size;
    };
  };

  public JButton getButton() {
    return button;
  }

  public abstract JComponent getClientComponent();

  public EllipsisPanel() {
    setBorder(null);
    setLayout(new BorderLayout());
    setBackground(Color.white);

    //textField.setBorder(null);
    add(getClientComponent());
    //按下Space键时模拟按钮单击
    addKeyListener(new KeyAdapter() {
      @Override
      public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_SPACE) {
          button.doClick();
        }
      }
    });

    button.setFocusable(false);
    button.setMargin(new Insets(0, 0, 0, 0));
    //button.setContentAreaFilled(false);
    //button.setBorderPainted(false);

    //button.setText("  ");

    add(button, BorderLayout.EAST);
    button.requestFocus();
  }
}
